/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.map.service.impl;

import me.zhengjie.modules.map.domain.MapArea;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.map.repository.MapAreaRepository;
import me.zhengjie.modules.map.service.MapAreaService;
import me.zhengjie.modules.map.service.dto.MapAreaDto;
import me.zhengjie.modules.map.service.dto.MapAreaQueryCriteria;
import me.zhengjie.modules.map.service.mapstruct.MapAreaMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2022-07-30
**/
@Service
@RequiredArgsConstructor
public class MapAreaServiceImpl implements MapAreaService {

    private final MapAreaRepository mapAreaRepository;
    private final MapAreaMapper mapAreaMapper;

    @Override
    public Map<String,Object> queryAll(MapAreaQueryCriteria criteria, Pageable pageable){
        Page<MapArea> page = mapAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(mapAreaMapper::toDto));
    }

    @Override
    public List<MapAreaDto> queryAll(MapAreaQueryCriteria criteria){
        return mapAreaMapper.toDto(mapAreaRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public MapAreaDto findById(Integer id) {
        MapArea mapArea = mapAreaRepository.findById(id).orElseGet(MapArea::new);
        ValidationUtil.isNull(mapArea.getId(),"MapArea","id",id);
        return mapAreaMapper.toDto(mapArea);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MapAreaDto create(MapArea resources) {
        return mapAreaMapper.toDto(mapAreaRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MapArea resources) {
        MapArea mapArea = mapAreaRepository.findById(resources.getId()).orElseGet(MapArea::new);
        ValidationUtil.isNull( mapArea.getId(),"MapArea","id",resources.getId());
        mapArea.copy(resources);
        mapAreaRepository.save(mapArea);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            mapAreaRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<MapAreaDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MapAreaDto mapArea : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("区域名称", mapArea.getName());
            map.put("颜色类型", mapArea.getColor());
            map.put("所属矿图", mapArea.getMineMapId());
            map.put("坐标", mapArea.getPoint());
            map.put("备注", mapArea.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}